<template>
  <div class="equipment-emission">
    <div class="section-title">
      <h3>{{ usagePhase === '拆除处置阶段' ? '废物运输碳排放' : '材料运输的碳排放' }}</h3>
      <div class="toolbar">
        <el-button type="success" size="small" @click="addMaterial" icon="el-icon-plus">新增运输</el-button>
        <el-button type="primary" size="small" @click="batchEdit" icon="el-icon-check">保存运输</el-button>
        <el-button type="danger" size="small" @click="clearAll" icon="el-icon-delete">清空运输</el-button>
      </div>
    </div>

    <el-table :data="transportData" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="序号" width="80">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="usagePhase === '拆除处置阶段' ? '废物名称' : '建材名称'" prop="materialName" width="80px">
        <template slot-scope="scope">
          {{ scope.row.materialName }}
        </template>
      </el-table-column>
      <el-table-column :label="usagePhase === '拆除处置阶段' ? '废弃物数量' : '用量'" prop="quantity">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.quantity" size="small" :min="0" :controls="false" />
        </template>
      </el-table-column>
      <el-table-column label="单位" prop="unit" width="80px">
        <template slot-scope="scope">
          {{ scope.row.unit }}
        </template>
      </el-table-column>
      <el-table-column :label="usagePhase === '拆除处置阶段' ? '废弃物碳排放因子' : '建材碳排放因子'" prop="carbonFactor" v-if="usagePhase !== '拆除处置阶段'" width="80px">
        <template slot-scope="scope">
          {{ scope.row.carbonFactor }}
        </template>
      </el-table-column>
      <el-table-column label="质量换算系数t/单位" prop="conversionFactor">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.conversionFactor" size="small" :min="0" :precision="2" :controls="false" />
        </template>
      </el-table-column>
      <el-table-column label="运输方式" prop="transportMode" width="90px">
        <template slot-scope="scope">
          {{ scope.row.transportMode }}
        </template>
      </el-table-column>
      <el-table-column label="运输距离(km)" prop="transportDistance">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.transportDistance" size="small" :min="0" :precision="2" :controls="false" />
        </template>
      </el-table-column>
      <el-table-column label="运输方式排放因子(kgCO2e/(t-km))" prop="transportFactor" width="80px">
        <template slot-scope="scope">
          {{ scope.row.transportFactor }}
        </template>
      </el-table-column>
      <el-table-column label="碳排放量" prop="emission">
        <template slot-scope="scope">
          {{ usagePhase === '拆除处置阶段' ? (scope.row.quantity * scope.row.conversionFactor * scope.row.transportDistance * scope.row.transportFactor).toFixed(2) :
          (scope.row.quantity * scope.row.carbonFactor + scope.row.quantity * scope.row.transportDistance * scope.row.conversionFactor * scope.row.transportFactor).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button size="small" type="danger" @click="removeEquipment(scope.row.materialId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { updateMasterialTransport, delMasterialTransport } from "@/api/business/masterialTransport"

export default {
  props: {
    transportData: {
      type: Array,
      default: () => []
    },
    usagePhase: {
      type: String,
      default: ""
    },
  },

  data() {
    return {
      selectedTransport: []
    }
  },
  created() {
  },

  watch: {
  },

  methods: {
    addMaterial() {
      // 通知父组件打开窗口
      this.$emit('openMaterialDialog')
    },

    removeEquipment(materialId) {
      // 删除设备
      const materialIds = materialId
      this.$modal.confirm('是否确认删除设备信息编号为"' + materialIds + '"的数据项？').then(function() {
        return delMasterialTransport(materialIds)
      }).then(() => {
        this.$emit('refreshTransportInfo')
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },

    batchEdit() {
      // 生成所有请求的 Promise 数组
      const promises = this.transportData.map(item =>
        updateMasterialTransport(item.materialId, item)
      );

      // 等待所有请求完成
      Promise.all(promises)
        .then(() => {
          this.$modal.msgSuccess("全部修改成功");
          this.$emit('refreshTransportInfo');
        })
    },

    clearAll() {
      // 获取所有id，删除设备
      const materialIds = this.transportData.map((item) => item.materialId)
      this.$modal.confirm('是否确认删除设备信息编号为"' + materialIds + '"的数据项？').then(function() {
        return delMasterialTransport(materialIds)
      }).then(() => {
        this.$emit('refreshTransportInfo')
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },

    handleSelectionChange(selection) {
      this.selectedTransport = selection
    }
  }
}
</script>

<style scoped>
.equipment-emission {
  margin-bottom: 24px;
  padding: 20px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.section-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.toolbar {
  display: flex;
  gap: 10px;
}
</style>
